/** @file

  Copyright (c) 2018, Linaro Ltd. All rights reserved.<BR>
  Copyright (C) 2022 - 2023, Phytium Technology Co., Ltd. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/
#include <IndustryStandard/Acpi.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/IoLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Protocol/AcpiTable.h>
#include <ArmPlatform.h>
#include <Library/HobLib.h>
#include <OEMSvc.h>

#define FIELD_OFFSET(type, name)            __builtin_offsetof(type, name)

#pragma pack(1)
typedef struct {
  EFI_ACPI_6_2_PPTT_STRUCTURE_PROCESSOR                     Core;
  UINT32                                                    Offset[2];
  EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE                         DCache;
  EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE                         ICache;
} PHYTIUM_PPTT_CORE;

typedef struct {
  EFI_ACPI_6_2_PPTT_STRUCTURE_PROCESSOR                     Cluster;
  UINT32                                                    Offset[1];
  EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE                         L2Cache;
  PHYTIUM_PPTT_CORE                                         Cores[2];
} PHYTIUM_PPTT_CLUSTER;

typedef struct {
  EFI_ACPI_6_2_PPTT_STRUCTURE_PROCESSOR                     Package;
  UINT32                                                    Offset[2];
  EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE                         L3Cache;
  EFI_ACPI_6_2_PPTT_STRUCTURE_ID                            ID;
  PHYTIUM_PPTT_CLUSTER                                      Clusters[4];
} PHYTIUM_PPTT_PACKAGE;

typedef struct {
  EFI_ACPI_6_2_PROCESSOR_PROPERTIES_TOPOLOGY_TABLE_HEADER   Pptt;
  PHYTIUM_PPTT_PACKAGE                                      Package[1];
} PHYTIUM_PPTT_TABLE;
#pragma pack()

#define PPTT_CORE(pid, cid, id) {                                             \
  {                                                                           \
    EFI_ACPI_6_2_PPTT_TYPE_PROCESSOR,                                         \
    FIELD_OFFSET (PHYTIUM_PPTT_CORE, DCache),                                 \
    {},                                                                       \
    {                                                                         \
      0,                                        /* PhysicalPackage */         \
      EFI_ACPI_6_2_PPTT_PROCESSOR_ID_VALID,     /* AcpiProcessorIdValid */    \
    },                                                                        \
    FIELD_OFFSET (PHYTIUM_PPTT_TABLE,                                         \
                  Package[pid].Clusters[cid]),      /* Parent */              \
    2 * (cid) + (id),                          /* AcpiProcessorId */          \
    2,                                         /* NumberOfPrivateResources*/  \
  }, {                                                                        \
    FIELD_OFFSET (PHYTIUM_PPTT_TABLE,                                         \
                  Package[pid].Clusters[cid].Cores[id].DCache),               \
    FIELD_OFFSET (PHYTIUM_PPTT_TABLE,                                         \
                  Package[pid].Clusters[cid].Cores[id].ICache),               \
  }, {                                                                        \
    EFI_ACPI_6_2_PPTT_TYPE_CACHE,                                             \
    sizeof (EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE),                               \
    {},                                                                       \
    {                                                                         \
      1,          /* SizePropertyValid */                                     \
      1,          /* NumberOfSetsValid */                                     \
      1,          /* AssociativityValid */                                    \
      1,          /* AllocationTypeValid */                                   \
      1,          /* CacheTypeValid */                                        \
      1,          /* WritePolicyValid */                                      \
      1,          /* LineSizeValid */                                         \
    },                                                                        \
    0,            /* NextLevelOfCache */                                      \
    SIZE_32KB,    /* Size */                                                  \
    256,          /* NumberOfSets */                                          \
    2,            /* Associativity */                                         \
    {                                                                         \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_ALLOCATION_READ_WRITE,                    \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_CACHE_TYPE_DATA,                          \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_WRITE_POLICY_WRITE_BACK,                  \
    },                                                                        \
    64            /* LineSize */                                              \
  }, {                                                                        \
    EFI_ACPI_6_2_PPTT_TYPE_CACHE,                                             \
    sizeof (EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE),                               \
    {},                                                                       \
    {                                                                         \
      1,          /* SizePropertyValid */                                     \
      1,          /* NumberOfSetsValid */                                     \
      1,          /* AssociativityValid */                                    \
      1,          /* AllocationTypeValid */                                   \
      1,          /* CacheTypeValid */                                        \
      0,          /* WritePolicyValid */                                      \
      1,          /* LineSizeValid */                                         \
    },                                                                        \
    0,            /* NextLevelOfCache */                                      \
    SIZE_32KB,    /* Size */                                              \
    256,          /* NumberOfSets */                                          \
    2,            /* Associativity */                                         \
    {                                                                         \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_ALLOCATION_READ,    /* AllocationType */  \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_CACHE_TYPE_INSTRUCTION,                   \
      0,                                                /* WritePolicy */     \
    },                                                                        \
    64            /* LineSize */                                              \
  }                                                                           \
}

#define PPTT_CLUSTER(pid, cid) {                                              \
  {                                                                           \
    EFI_ACPI_6_2_PPTT_TYPE_PROCESSOR,                                         \
    FIELD_OFFSET (PHYTIUM_PPTT_CLUSTER, L2Cache),                             \
    {},                                                                       \
    {                                                                         \
      0,                                      /* PhysicalPackage */           \
      EFI_ACPI_6_2_PPTT_PROCESSOR_ID_INVALID, /* AcpiProcessorIdValid */      \
    },                                                                        \
    FIELD_OFFSET (PHYTIUM_PPTT_TABLE, Package[pid]), /* Parent */             \
    0,                                        /* AcpiProcessorId */           \
    1,                                        /* NumberOfPrivateResources */  \
  }, {                                                                        \
    FIELD_OFFSET (PHYTIUM_PPTT_TABLE, Package[pid].Clusters[cid].L2Cache),    \
  }, {                                                                        \
    EFI_ACPI_6_2_PPTT_TYPE_CACHE,                                             \
    sizeof (EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE),                               \
    {},                                                                       \
    {                                                                         \
      1,          /* SizePropertyValid */                                     \
      1,          /* NumberOfSetsValid */                                     \
      1,          /* AssociativityValid */                                    \
      1,          /* AllocationTypeValid */                                   \
      1,          /* CacheTypeValid */                                        \
      1,          /* WritePolicyValid */                                      \
      1,          /* LineSizeValid */                                         \
    },                                                                        \
    0,            /* NextLevelOfCache */                                      \
    SIZE_2MB,   /* Size */                                                    \
    2048,          /* NumberOfSets */                                         \
    16,           /* Associativity */                                         \
    {                                                                         \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_ALLOCATION_READ_WRITE,                    \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_CACHE_TYPE_UNIFIED,                       \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_WRITE_POLICY_WRITE_BACK,                  \
    },                                                                        \
    64            /* LineSize */                                              \
  }, {                                                                        \
    PPTT_CORE(pid, cid, 0),                                                   \
    PPTT_CORE(pid, cid, 1),                                                   \
  }                                                                           \
}

STATIC PHYTIUM_PPTT_TABLE mPhytiumPpttTable = {
  {
    ARM_ACPI_HEADER(EFI_ACPI_6_2_PROCESSOR_PROPERTIES_TOPOLOGY_TABLE_STRUCTURE_SIGNATURE,
                  PHYTIUM_PPTT_TABLE,
                  EFI_ACPI_6_2_PROCESSOR_PROPERTIES_TOPOLOGY_TABLE_REVISION),
  },
  {
    {
      {
        EFI_ACPI_6_2_PPTT_TYPE_PROCESSOR,
        FIELD_OFFSET (PHYTIUM_PPTT_PACKAGE, L3Cache),
        {},
        {
          1,                                      /* PhysicalPackage */
          EFI_ACPI_6_2_PPTT_PROCESSOR_ID_INVALID, /* AcpiProcessorIdValid */
        },
        0,                                        /* Parent */
        0,                                        /* AcpiProcessorId */
        2,                                        /* NumberOfPrivateResources */
      }, {
        FIELD_OFFSET (PHYTIUM_PPTT_TABLE, Package[0].L3Cache),
        FIELD_OFFSET (PHYTIUM_PPTT_TABLE, Package[0].ID),
      }, {
        EFI_ACPI_6_2_PPTT_TYPE_CACHE,
        sizeof (EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE),
        {},
        {
          1,                                      /* SizePropertyValid */
          1,                                      /* NumberOfSetsValid */
          1,                                      /* AssociativityValid */
          0,                                      /* AllocationTypeValid */
          1,                                      /* CacheTypeValid */
          1,                                      /* WritePolicyValid */
          1,                                      /* LineSizeValid */
        },
        0,                                        /* NextLevelOfCache */
        SIZE_4MB,                                 /* Size */
        4096,                                     /* NumberOfSets */
        16,                                       /* Associativity */
        {
          0,
          EFI_ACPI_6_2_CACHE_ATTRIBUTES_CACHE_TYPE_UNIFIED,
          EFI_ACPI_6_2_CACHE_ATTRIBUTES_WRITE_POLICY_WRITE_BACK,
        },
        64                                        /* LineSize */
      }, {
        EFI_ACPI_6_2_PPTT_TYPE_ID,
        sizeof(EFI_ACPI_6_2_PPTT_STRUCTURE_ID),
        {0},
        0x54594850,
        0x5,
        0x0,
        0,
        0,
        0,
      }, {
        PPTT_CLUSTER (0, 0),
        PPTT_CLUSTER (0, 1),
        PPTT_CLUSTER (0, 2),
        PPTT_CLUSTER (0, 3),
      }
    }
  }
};

EFI_STATUS
EFIAPI
PpttEntryPoint (
  IN EFI_HANDLE          ImageHandle,
  IN EFI_SYSTEM_TABLE    *SystemTable
)
{
	EFI_STATUS Status = EFI_SUCCESS;
	UINTN          AcpiTableHandle;
	EFI_ACPI_TABLE_PROTOCOL        *AcpiTableProtocol = NULL;
    PHYTIUM_CPU_INFO				*CpuInfo;
	EFI_HOB_GUID_TYPE		  *GuidHob;
    UINTN CpuVersion;

	GuidHob = GetFirstGuidHob(&gPlatformCpuInforGuid);
	if (GuidHob == NULL){
		DEBUG((EFI_D_ERROR,"%a(),Get cpu info hob failed!\n",__FUNCTION__));
		return EFI_UNSUPPORTED;
	}
	CpuInfo = (PHYTIUM_CPU_INFO *) GET_GUID_HOB_DATA(GuidHob);
    //[jdzhang-0009]>>>
    //CpuVersion = CpuInfo->CupVersion;
    CpuVersion = CpuInfo->CpuVersion;
    //[jdzhang-0009]<<<

    mPhytiumPpttTable.Package[0].ID.Level1Id = CpuVersion >> 8 & 0xFF;
    mPhytiumPpttTable.Package[0].ID.Level2Id = CpuVersion  & 0xFF;

	Status = gBS->LocateProtocol (
                  &gEfiAcpiTableProtocolGuid,
                  NULL,
                  (VOID**)&AcpiTableProtocol);
    if (EFI_ERROR (Status)) {
      return Status;
  	}

	Status = AcpiTableProtocol->InstallAcpiTable (
                                 AcpiTableProtocol,
                                 &mPhytiumPpttTable,
                                 mPhytiumPpttTable.Pptt.Header.Length,
                                 &AcpiTableHandle);
    ASSERT_EFI_ERROR (Status);
	return Status;
}

